<!--
 * @Description: 侧边菜单栏模板页面
 * @Date: 2023-03-29 15:37:31
 * @LastEditTime: 2023-04-05 20:05:49
 * @Author: Black
 * @FilePath: \store_admin\src\layouts\components\fragments_menu.vue
-->
<template>
  <div class="fragments_menu" :style="{ width:$store.state.asideWidth }">
    <el-menu unique-opened
      :default-active="defaultActive"
      :collapse="isCollapse" 
      :collapse-transition="false" 
      default-active="2" class="border-0" @select="handleSelect">
      <!-- 一级菜单遍历 -->
      <template v-for="(item, index) in asideMenus" :key="index">
        <!-- 一级菜单的条件判断 -->
        <el-sub-menu v-if="item.child && item.child.length > 0" :index="item.name">
          <template #title>
            <el-icon>
              <!-- 设置动态图标icon -->
              <component :is="item.icon"></component>
            </el-icon>
            <span>{{ item.name }}</span>
          </template>
          <!-- 二级菜单遍历 -->
          <el-menu-item v-for="(item2, index2) in item.child" :key="index2" :index="item2.frontpath">
            <el-icon>
              <component :is="item2.icon"></component>
            </el-icon>
            <span>{{ item2.name }}</span>
          </el-menu-item>
        </el-sub-menu>
        <!-- 一级菜单的条件判断后其他一级菜单遍历结果存放处 -->
        <el-menu-item v-else :index="item.frontpath">
          <el-icon>
            <component :is="item.icon"></component>
          </el-icon>
          <span>{{ item.name }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script setup>
import { useRouter, useRoute, onBeforeRouteUpdate } from 'vue-router'
import { computed, ref } from 'vue'
import { useStore } from 'vuex';

const router = useRouter()
const route = useRoute()
const store = useStore()

// 默认选中
const defaultActive = ref(route.path)

// 监听路由变化 实现和标签栏路由联动跳转
onBeforeRouteUpdate((to, from) => {
  defaultActive.value = to.path
})

// 是否折叠
const asideMenus = computed(() => store.state.menus)

const handleSelect = (e) => {
  // console.log(e) // 获取到的是 path 路径
  router.push(e)
}

// 是否折叠侧边菜单栏
const isCollapse = computed(() => !(store.state.asideWidth == '250px'))

</script>

<style scoped>
.fragments_menu {
  width: 250px;
  transition: all 0.2s;
  top: 64px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  @apply shadow-md fixed bg-light-50;
}

.fragments_menu::-webkit-scrollbar {
  width: 0px;
}
</style>